package com.kfm.jdbc.day04;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 创建 c3p0 数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 设置连接数据库的参数
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/company?serverTimezone=UTC");
        dataSource.setUser("root");
        dataSource.setPassword("");

        // 设置连接池参数
        dataSource.setInitialPoolSize(5);
        dataSource.setMaxPoolSize(10);

        // 获取 Connection 对象
        Connection connection = dataSource.getConnection();
        String sql = "select * from dept";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        ResultSet resultSet = preparedStatement.executeQuery();
        // 结果集 元信息。 列名，列的个数，列的类型
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()){
            for (int i = 1; i <= columnCount ; i++) {
                System.out.print(resultSet.getObject(i) + "\t");
            }
            System.out.println();
        }

        // 释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
